import requests
import json
import subprocess
import os
from urllib3.exceptions import InsecureRequestWarning
import datetime

today = datetime.date.today()  # 今天
yester = today - datetime.timedelta(days=1)
# 禁用安全请求警告
Platform = "追书云"


def zhuishuyun():
    try:
        requests.urllib3.disable_warnings(InsecureRequestWarning)
        r = requests.session()
        ff = r.get(url="https://channel.zhuishuyun.com/api/auth/captcha", verify=False, timeout=15)
        with open("capt002.jpg", "wb") as f:
            f.write(ff.content)
            # 关闭文件流对象
            f.close()
        subprocess.call('capt002.jpg', shell=True)
        captcha = input("请输入验证码：")
        date = {
            "phone": "",
            "password": "",
            "captcha": captcha,
        }
        date = dict(date)
        os.remove("capt002.jpg")
        rr = r.post(url="https://channel.zhuishuyun.com/api/auth/login", data=date, verify=False, timeout=15)
        aa = r.get(url="https://channel.zhuishuyun.com/api/trade/orderDayStats", verify=False, timeout=15)
        html = json.loads(aa.text)
        for y in range(len(html["data"]["list"])):
            if str(html["data"]["list"][y]["date"]) == str(yester):
                Amount_share = html["data"]["list"][y]["total_recharge_amount"]
                Amount_share = round(float(Amount_share) * 0.9, 2)
                date_time = str(yester)
                return date_time, Amount_share, Platform
            else:
                if y == len(html["data"]["list"]) - 1:
                    return '', 'None', Platform
                else:
                    pass
    except Exception:
        print("获取 追书云 数据失败")
        return '', 'None', Platform
    except ConnectionError:
        print("获取 追书云 数据 超时")
        return '', 'None', Platform
